
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;
import com.huizhan.web.view.*;

/**
 * 邀约模板api
 */
 
@Api(tags = "Invite",description = "邀约模板")
@RestController
public class InviteController {
    
    @Autowired
    private InviteDao dao;
   
    /**
     * 获取邀约模板集合
     * @return api结果
     */
    @ApiOperation(value = "根据项目ID获取邀约模板集合")
    @RequestMapping(value =  "/api/Invite/Project/{id}",method = {RequestMethod.GET})
    public ApiResult<List<Invite>> Get(@PathVariable String id){
        ApiResult<List<Invite>> result=new ApiResult<List<Invite>>();
        try 
        {
            List<Invite> res = dao.findByProjectId(id);
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取邀约模板信息
     * @param id 邀约模板ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取邀约模板信息")
    @RequestMapping(value =  "/api/Invite/{id}",method = {RequestMethod.GET})
    public ApiResult<Invite> Get(@PathVariable Long id){
        ApiResult<Invite> result=new ApiResult<Invite>();
        try
        {
            Invite model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改邀约模板信息
     * @param id 邀约模板ID
     * @param model 邀约模板信息
     * @return api结果
     */
    @ApiOperation(value = "修改邀约模板信息")
    @RequestMapping(value =  "/api/Invite/{id}",method = {RequestMethod.PUT})
    public ApiResult<Invite> Edit(@PathVariable Long id,@RequestBody Invite model){
        ApiResult<Invite> result=new ApiResult<Invite>();
        try
        {
            model.setInviteId(id);
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加邀约模板信息
     * @param model 邀约模板信息
     * @return api结果
     */
    @ApiOperation(value = "添加邀约模板信息")
    @RequestMapping(value =  "/api/Invite/",method = {RequestMethod.POST})
    public ApiResult<Invite> Add(@RequestBody Invite model){
         ApiResult<Invite> result=new ApiResult<Invite>();
        try
        {
            if  (model.getProjectId()==""){
                result.setHasError(true);
                result.setMessage("项目ID不能为空");
            }else{ 
                model.setCreateDate(new Date());
                dao.save(model);
                result.setData(model);
           }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除邀约模板信息
     * @param id 邀约模板ID
     * @return api结果
     */
    @ApiOperation(value = "删除邀约模板信息")
    @RequestMapping(value =  "/api/Invite/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 查询邀约模板种集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询邀约模板种集合")
    @RequestMapping(value =  "/api/Invite/Search",method = {RequestMethod.POST})
    public ApiResult<List<Invite>> Search(@RequestBody PageProject model){
         ApiResult<List<Invite>> result=new ApiResult<List<Invite>>();
         
        try
        {
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"create_date") ;            
            Page<Invite> page = dao.findByAll(model.getName(),model.getType(),model.getProjectId(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

}

